import {
  getEmpList,
  probEmpList,
  empTabNum,
  getProDetail,
  getFeedback,
  getEmpNameList,
  getAdjInsDetail,
  getInsIdeList,
  getSocialDetail,
  getAccumulationDetail,
  getPersonChargeDetail,
} from '@/services/adjusttask';
import { message } from 'antd';

export default {
  state: {
    data: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    batchQuery: {
      // 批量上传数据
      sendType: '1',
      isSerSetFlag: '1',
      progressPercent: 0,
    },
    proBaseList: {},
    proFbList: [],
    probId: '',
    empNameList: [], // 单个调整雇员姓名
    adjustInfo: {
      updateBaseInfo: {}, // 基本信息
      updateInsIde: null, // 缴费人员类别
      updateInsAndAccuLocPkgList: null, // 社保组合
      updateInsPdDecWage: null, // 社保申报工资
      isInsIdeFlag: false, // 是否修改缴费人员类别
      insIdeList: [], // 缴费人员类别列表
      isInsDecWageFlag: false, // 是否修改社保申报工资
    }, // 调整信息
    getSocialDetailData: {},
  },
  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getEmpList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getList',
          payload: {
            data: [],
            currentPage,
            pageSize,
          },
        });
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取问题人员列表
    *fetchProbList({ payload }, { call, put }) {
      const response = yield call(probEmpList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        yield put({
          type: 'getList',
          payload: {
            data: [],
            currentPage,
            pageSize,
          },
        });
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取页签数量
    *fetchTabNum({ payload, callback }, { call }) {
      const response = yield call(empTabNum, payload);
      if (response.success) {
        if (callback && typeof callback === 'function') {
          callback(response.data);
        }
      }
    },
    // 获取问题人员详情
    *fetchProDetailList({ payload }, { call, put }) {
      const response = yield call(getProDetail, payload);
      if (response.success) {
        yield put({
          type: 'getProDetail',
          payload: response.data,
        });
      }
    },
    // 问题人员反馈
    *proFeedBack({ payload, callback }, { call }) {
      const response = yield call(getFeedback, payload);
      if (response.success) {
        if (callback) {
          callback(response);
        }
      } else {
        message.warning(`${response.message || '反馈失败'}`);
      }
    },
    // 问题人员反馈并取消标记
    *cancelProbFlag({ payload, callback }, { call }) {
      const response = yield call(getFeedback, payload);
      if (callback) {
        callback(response);
      }
    },
    // 单个调整：输入姓名,证件号模糊查询
    *fetchEmpNameList({ payload }, { call, put }) {
      const response = yield call(getEmpNameList, payload);
      if (response.success) {
        yield put({
          type: 'getEmpNameList',
          payload: response.data,
        });
        return response.data;
      }
    },
    // 单个调整：获取社保信息
    *fetchAdjInsDetail({ payload }, { call, put, select }) {
      const response = yield call(getAdjInsDetail, payload);
      const adjustInfo = yield select(state => state.adjusttask.adjustInfo);
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: { adjustInfo: { ...adjustInfo, ...payload, ...response.data } },
        });
      }
    },
    // 获取缴费人员列表
    *fetchInsIdeList({ payload }, { call, put, select }) {
      const response = yield call(getInsIdeList, payload);
      const adjustInfo = yield select(state => state.adjusttask.adjustInfo);
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'updateModelData',
            payload: {
              adjustInfo: {
                ...adjustInfo,
                insIdeList: response.data,
              },
            },
          });
        } else {
          message.warning('该地区下未维护缴费人员');
        }
      } else {
        message.warning(response.message || '获取缴费人员列表：服务端错误');
      }
    },
    // 社保查看详情
    *fetchSocialDetail({ payload }, { call, put }) {
      const response = yield call(getSocialDetail, payload);
      if (response.success) {
        yield put({
          type: 'getSocialDetail',
          payload: response.data,
        });
      }
    },
    // 公积金查看详情
    *fetchAccumulationDetail({ payload }, { call, put }) {
      const response = yield call(getAccumulationDetail, payload);
      if (response.success) {
        yield put({
          type: 'getSocialDetail',
          payload: response.data,
        });
      }
    },
    // // 人员及收费详情
    // fetchPersonChargeDetail({ payload }, { call, put }) {
    //   const response = yield call(getPersonChargeDetail, payload);
    //   if (response.success) {
    //     yield put({
    //       type: 'getSocialDetail',
    //       payload: response.data,
    //     });
    //   }
    // },
  },

  reducers: {
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 问题人员详情
    getProDetail(state, { payload }) {
      return {
        ...state,
        proBaseList: payload,
        proFbList: payload.probFdbkList,
        probId: payload.probId,
      };
    },
    // 单个调整：雇员姓名
    getEmpNameList(state, { payload }) {
      return {
        ...state,
        empNameList: payload,
      };
    },
    // 社保详情
    getSocialDetail(state, { payload }) {
      return {
        ...state,
        getSocialDetailData: payload,
      };
    },
  },
};
